package com.moleq.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.moleq.base.MoleQBaseDialog;
import com.moleq.logic.TransactionManager;
import com.moleq.mgpos.activities.R;
import com.moleq.mgpos.activities.SaleActivity;
import com.moleq.mgpos.widget.NumberPadButton;
import com.moleq.mgpos.widget.NumberPadForPayment;
import com.moleq.mgpos.widget.NumberPadForPayment.NumberPadListener;
import com.moleq.models.FreeFuncRpt;
import com.moleq.models.GlobalInfo;

public class TareInfoDialog extends MoleQBaseDialog {

	@Override
	public void show() {
		// TODO Auto-generated method stub
		super.show();

		mNumPad.setBuffer("");
	}

	private Button btnTare;
	private TransactionManager mPaymentMgr;
	private SaleActivity sa;
	private Button.OnClickListener mOnClickListener = new Button.OnClickListener() {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			switch (arg0.getId()) {
			case R.id.buttonTareInfoClose:
				dismiss();
				break;
			case R.id.buttonTareInfoConfirm:
				GlobalInfo.getInstance().CurrentFreeFuncID = FreeFuncRpt.FF_TARE;
				setTare();
				dismiss();
				break;
			}
		}

	};
	private Button btnClose;
	private NumberPadForPayment mNumPad;
	private NumberPadListener mNumPadListener = new NumberPadListener() {

		@Override
		public void onClick(NumberPadButton button) {
			switch (button) {
			case ENTER:
				setTare();
				dismiss();
				break;
			case DOT:
				String qty = mNumPad.getBuffer();
				qty = qty.replace(".", "");
				mNumPad.setBuffer(qty);
				break;
			}

		}

	};

	public void setTare() {

		String strAuth = mNumPad.getBuffer();

		if (strAuth != null && !strAuth.equals("")) {
			GlobalInfo.getInstance().CurrentFreeFuncID = FreeFuncRpt.FF_TARE;
			if (strAuth.length() > 4) {
				MessageBox.show(mContext, "Entered amount is too large.");
				return;
			}
			mPaymentMgr.manualTare = Integer.valueOf(strAuth);
			sa.refreshDisplayContent();
		}

	}

	public TareInfoDialog(Context context, Object base) {
		super(context, R.style.dialog, base);
	}

	public TareInfoDialog(Context context) {
		super(context, R.style.dialog, context);
		sa = (SaleActivity) context;
	}

	public TareInfoDialog(Context context, int theme, Object base) {
		super(context, theme, base);
	}

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.dialog_input_tare_info);

		mPaymentMgr = TransactionManager.getInstance();

		btnTare = (Button) findViewById(R.id.buttonTareInfoConfirm);
		btnTare.setOnClickListener(mOnClickListener);

		btnClose = (Button) findViewById(R.id.buttonTareInfoClose);
		btnClose.setOnClickListener(mOnClickListener);

		mNumPad = (NumberPadForPayment) findViewById(R.id.authNumPad);
		mNumPad.setNumberPadListener(mNumPadListener);
		mNumPad.setStyle(NumberPadForPayment.SALE_STYLE);
		this.setCancelable(false);

	}

}
